﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Manager.App_Code;
using Manager.Common;
using System.Linq;
using Manager.ViewModels;

namespace Manager.FormAdd_Edit
{
    public partial class frm_HoaDonDichVu : DevExpress.XtraEditors.XtraForm
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        List<DichVuViewModel> lstDichVu;
        DichVu dichVu = null;
        double TongThanhTienVND = 0; //tong tien tinh theo VND
        public string MaPhong { get; set; }

        public frm_HoaDonDichVu()
        {
            MaPhong = "";
            InitializeComponent();
        }        

        public frm_HoaDonDichVu(string _MaPhong)
        {
            InitializeComponent();
            MaPhong = _MaPhong;
        }
        
        private void frm_HoaDonDichVu_Load(object sender, EventArgs e)
        {
            try
            {
                lstDichVu = new List<DichVuViewModel>();
                dateNgayLap.Text = DateTime.Now.ToShortDateString();
                if (db.PhuThuDichVus.ToList().Count == 0)
                    txtMaĐK.Text = "1";
                else
                    txtMaĐK.Text = (db.PhuThuDichVus.Max(i => i.MaPhuThu) + 1).ToString();

                var lstNhomDV = db.NhomDichVus.ToList();
                comLoaiDichVu.DataSource = lstNhomDV;
                comLoaiDichVu.DisplayMember = "TenNhomDV";
                comLoaiDichVu.ValueMember = "MaNhomDV";
                comLoaiDichVu.SelectedIndex = 0;

                cmbTienTe.DataSource = db.TienTes.ToList();
                cmbTienTe.DisplayMember = "TenDonVi";
                cmbTienTe.ValueMember = "MaDonVi";
                var tienTe = db.TienTes.SingleOrDefault(t => t.MaDonVi == "VND");
                if (tienTe != null)
                    cmbTienTe.SelectedValue = tienTe.MaDonVi;

                if (MaPhong != "")
                {
                    var phieuDangKy = db.PhieuDangKyPhongs.SingleOrDefault(i => i.ChiTietPhieuDangKy.MaPhong == MaPhong && i.TinhTrang == false);
                    txtMaPhong.Text = phieuDangKy.ChiTietPhieuDangKy.MaPhong;
                    txtMaPhieuDK.Text = phieuDangKy.MaPhieu.ToString();
                    txtTenKhachHang.Text = phieuDangKy.ChiTietPhieuDangKy.KhachHang.TenKH;
                }
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_HoaDonDichVu:frm_HoaDonDichVu_Load " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
            }
        }

        private void comLoaiDichVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            var nhomDichVu = comLoaiDichVu.SelectedItem as NhomDichVu;
            if (nhomDichVu != null)
            {                
                var lstDV = db.DichVus.Where(i => i.NhomDichVu1 == nhomDichVu);
                gridCT_DV.DataSource = lstDV;
            }
        }        

        private void gridCT_DV_DoubleClick(object sender, EventArgs e)
        {
            int index = gridV_DV.FocusedRowHandle;
            dichVu = gridV_DV.GetRow(index) as DichVu;
            frm_ThemDichVu themDV = new frm_ThemDichVu(dichVu);
            themDV.FormClosed += new FormClosedEventHandler(themDV_FormClosed);
            themDV.ShowDialog();            
        }

        void themDV_FormClosed(object sender, FormClosedEventArgs e)
        {
            var themDV = (frm_ThemDichVu)sender;
            if (themDV.SoLuong > 0)
            {                
                AddDichVu(dichVu, themDV.SoLuong);
            }
        }                        

        private void butLoadRoom_Click(object sender, EventArgs e)
        {
            frm_ListRoomRegistered listr = new frm_ListRoomRegistered();
            listr.FormClosed += new FormClosedEventHandler(listr_FormClosed);
            listr.ShowDialog();
        }

        void listr_FormClosed(object sender, FormClosedEventArgs e)
        {
            var temp = (frm_ListRoomRegistered)sender;
            txtMaPhieuDK.Text = temp._MaPhieuDangKy;
            txtMaPhong.Text = temp._MaPhong;
            txtTenKhachHang.Text = temp._TenKhachHang;
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            if (Save())
                this.Close();
        }

        private void butLuu_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void butThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }        

        private void frm_HoaDonDichVu_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F4)
                this.butSave_Click(sender, e);
            if (e.KeyCode == Keys.F2)
                this.butLuu_Click(sender, e);
            if (e.KeyCode == Keys.Escape)
                this.butThoat_Click(sender, e);

        }

        public void AddDichVu(DichVu dichVu, int soLuong)
        {
            if (dichVu == null)
                return;
            var temp = lstDichVu.SingleOrDefault(i => i.MaDV == dichVu.MaDV);
            if (temp != null)
                temp.SoLuong++;
            else
            {
                var vmDichVu = new DichVuViewModel();
                vmDichVu.MaDV = dichVu.MaDV;
                vmDichVu.LoaiDichVu = LoaiDichVu.CoDinh;
                vmDichVu.MaPhieuDangKy = long.Parse(txtMaPhieuDK.Text);
                vmDichVu.TenDichVu = dichVu.TenDichVu;
                vmDichVu.DonViTienTe = dichVu.DVTienTe;
                vmDichVu.GhiChu = dichVu.GhiChu;
                vmDichVu.GiaDV = dichVu.GiaDV;
                vmDichVu.NgayTao = dichVu.NgayTao;
                vmDichVu.NhomDichVu = dichVu.NhomDichVu;
                vmDichVu.TenDonViTienTe = dichVu.TienTe.TenDonVi;
                vmDichVu.TenNhomDichVu = dichVu.NhomDichVu1.TenNhomDV;
                vmDichVu.SoLuong = soLuong;
                vmDichVu.ThanhTien = vmDichVu.SoLuong * vmDichVu.GiaDV;
                vmDichVu.ThanhTienVND = vmDichVu.ThanhTien.Value * dichVu.TienTe.TyGiaThamKhao.Value;
                lstDichVu.Add(vmDichVu);
            }

            gridControl1.DataSource = null;
            gridControl1.DataSource = lstDichVu;

            double TongTien = 0;
            foreach (var vm in lstDichVu)
            {
                TongTien += vm.ThanhTienVND;
            }
            TongThanhTienVND = TongTien;
            txtTongTien.Text = CommonFunction.AddSeparator(TongTien);
            if (cmbTienTe.SelectedValue.ToString() != "VND")
                cmbTienTe.SelectedValue = "VND";
        }

        public bool Save()
        {
            try
            {
                if (txtMaPhong.Text == "")
                {
                    MessageBox.Show("Vui lòng chọn phòng thêm dịch vụ!");
                    return false;
                }
                else if (lstDichVu.Count <= 0)
                {
                    MessageBox.Show("Vui lòng chọn dịch vụ!");
                    return false;
                }
                else
                {
                    var lstPhuThuDichVu = new List<PhuThuDichVu>();                    
                    foreach (var vm in lstDichVu)
                    {                        
                        PhuThuDichVu phieuThuDichVu = new PhuThuDichVu();
                        //phieuThuDichVu.MaPhuThu = long.Parse(txtMaĐK.Text);
                        phieuThuDichVu.MaPhieuDangKy = long.Parse(txtMaPhieuDK.Text);
                        phieuThuDichVu.MaPhong = txtMaPhong.Text;
                        phieuThuDichVu.NhomDichVu = vm.NhomDichVu;                        
                        phieuThuDichVu.MaTienTe = vm.DonViTienTe;
                        phieuThuDichVu.NgayTao = DateTime.Now;
                        phieuThuDichVu.TenDichVu = vm.TenDichVu;
                        phieuThuDichVu.MaDichVu = vm.MaDV;
                        phieuThuDichVu.SoLuong = vm.SoLuong;
                        phieuThuDichVu.GiaTien = vm.GiaDV;
                        var thanhTien = Math.Round((phieuThuDichVu.SoLuong.Value * phieuThuDichVu.GiaTien.Value), 2);
                        phieuThuDichVu.ThanhTien = thanhTien;
                        //trừ hàng trong tạm ứng
                        if (vm.LoaiDichVu == LoaiDichVu.CoDinh)
                        {
                            var tamUng = db.TamUngs.SingleOrDefault(t => t.MaPhong == txtMaPhong.Text && vm.MaDV == t.MaDichVu);
                            if (tamUng != null)
                            {
                                if (tamUng.SoLuongTamUng < vm.SoLuong)
                                {
                                    MessageBox.Show(string.Format("Phòng {0} đã hết {1}, vui lòng nhập thêm.", txtMaPhong.Text, vm.TenDichVu));
                                    return false;
                                }
                                else
                                    tamUng.SoLuongTamUng -= vm.SoLuong;
                            }
                            else
                            {
                                MessageBox.Show(string.Format("Phòng {0} đã hết {1}, vui lòng nhập thêm.", txtMaPhong.Text, vm.TenDichVu));
                                return false;
                            }
                        }

                        lstPhuThuDichVu.Add(phieuThuDichVu);
                    }
                    if (lstPhuThuDichVu.Count > 0)
                    {
                        db.PhuThuDichVus.InsertAllOnSubmit(lstPhuThuDichVu);
                        db.SubmitChanges();
                        MessageBox.Show("Lưu dữ liệu thành công!");
                    }
                    else
                        MessageBox.Show("Vui lòng chọn dịch vụ!!!");                    
                }
                return true;
            }
            catch (Exception ex)
            {
                Log.WriteLog("frm_HoaDonDichVu:Save " + ex.Message + "\n" + ex.StackTrace);
                MessageBox.Show("Có lỗi xảy ra!!!");
                return false;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Xoa");
        }

        private void cmbTienTe_SelectedIndexChanged(object sender, EventArgs e)
        {
            var tienTe = cmbTienTe.SelectedItem as TienTe;
            if (tienTe != null)
            {                
                var tongTien = Math.Round((TongThanhTienVND / tienTe.TyGiaThamKhao.Value), 2);
                txtTongTien.Text = CommonFunction.AddSeparator(tongTien);
            }
        }

        private void btnThemDichVu_Click(object sender, EventArgs e)
        {
            frm_ThemDichVuKhac frm = new frm_ThemDichVuKhac();
            frm.FormClosed += new FormClosedEventHandler(frm_FormClosed);
            frm.ShowDialog();
        }

        void frm_FormClosed(object sender, FormClosedEventArgs e)
        {
            var frm = sender as frm_ThemDichVuKhac;
            if (frm.vmDichVu != null)
            {
                frm.vmDichVu.MaPhieuDangKy = CommonFunction.ParseLong(txtMaPhieuDK.Text);                
                lstDichVu.Add(frm.vmDichVu);
                gridControl1.DataSource = null;
                gridControl1.DataSource = lstDichVu;

                double TongTien = 0;
                foreach (var vm in lstDichVu)
                {
                    TongTien += vm.ThanhTienVND;
                }
                TongThanhTienVND = TongTien;
                txtTongTien.Text = CommonFunction.AddSeparator(TongTien);
                if (cmbTienTe.SelectedValue.ToString() != "VND")
                    cmbTienTe.SelectedValue = "VND";
            }
        }
      
    }
}